package cmd

import (
	"context"
	"fmt"

	"gitee.com/go-course/go8/projects/devcloud/mcenter/apps/domain"
	"gitee.com/go-course/go8/projects/devcloud/mcenter/apps/user"
	"github.com/infraboard/mcube/app"
	"github.com/spf13/cobra"
)

var (
	createTableFilePath string
)

// initCmd represents the start command
var initCmd = &cobra.Command{
	Use:   "init",
	Short: "mcenter 服务初始化",
	Long:  "mcenter 服务初始化",
	RunE: func(cmd *cobra.Command, args []string) error {
		// 初始化全局变量
		if err := loadGlobalConfig(confType); err != nil {
			return err
		}

		// 初始化全局日志配置
		if err := loadGlobalLogger(); err != nil {
			return err
		}

		// 初始化全局app
		if err := app.InitAllApp(); err != nil {
			return err
		}

		//  创建一个主账号(admin)
		us := app.GetInternalApp(user.AppName).(user.Service)
		creatUserReq := user.NewCreateUserRequest()
		creatUserReq.CreateBy = user.CREATE_BY_ADMIN
		creatUserReq.Description = "超级管理员"
		creatUserReq.Domain = domain.DEFAULT_DOMAIN
		creatUserReq.Type = user.TYPE_SUPPER
		creatUserReq.Username = "admin"
		creatUserReq.Password = "123456"
		u, err := us.CreateUser(context.Background(), creatUserReq)
		if err != nil {
			return err
		}

		fmt.Printf("初始化管理员用户: %s 成功\n", u.Spec.Username)
		return nil
	},
}

func init() {
	RootCmd.AddCommand(initCmd)
}
